from flask_socketio import SocketIO,send,emit,join_room,leave_room,rooms

from common.log.logUtils import logUtils


class socketService:

    # 定义信令
    #自己已加入房间
    SING_JOINED = 'joined'
    #别人加入房间
    SING_OTHER_JOINED = 'otherjoin'
    #自己离开房间
    SING_LEAVED = 'leaved'
    #别人离开房间信令
    SING_OTHER_LEAVE='bye'
    #普通信息
    SING_MESSAGE = "message"




    # 发送给房间内其他人
    @staticmethod
    def send_to_other(eventType, roomId, data):
        emit(eventType, (roomId,data), room=roomId, include_self=False)
        logUtils.info("send_to_other:eventType="+eventType+",roomId="+roomId+",data="+str(data))

    # 发送给自己
    @staticmethod
    def send_to_self(eventType, data):
        emit(eventType, data)
        logUtils.info("send_to_self eventType="+eventType+",data="+str(data))

    @staticmethod
    def send_to_self_with_roomId(eventType, roomId,data):
        emit(eventType, (roomId,data))
        logUtils.info("send_to_self eventType=" + eventType + ",data=" + str(data))

    # 发送给所有人
    @staticmethod
    def send_to_all(eventType, roomId, data):
        emit(eventType, data, room=roomId)
        logUtils.info("send_to_all:eventType="+eventType+",roomId="+roomId+",data="+str(data))
